<template>
  <div class="container">
    <div class="top">
      <el-input
        style="width: 150px; margin-bottom: 5px"
        v-model="search.code"
        placeholder="请输入单据编号"
      ></el-input>
      <el-select
        v-model="search.kind"
        multiple
        collapse-tags
        placeholder="请选择业务类型"
      >
        <el-option
          v-for="item in businessType"
          :key="item.value"
          :label="item.label"
          :value="item.value"
        ></el-option>
      </el-select>
      <el-select v-model="search.is_normal" placeholder="请选择结算类型">
        <el-option
          v-for="item in normalType"
          :key="item.value"
          :label="item.label"
          :value="item.value"
        ></el-option>
      </el-select>
      <el-input
        style="width: 300px"
        :disabled="true"
        placeholder="请搜索商品信息"
        v-model="commodityNameList.name"
      >
        <template slot="prepend">商品</template>
        <el-button
          slot="append"
          icon="el-icon-search"
          @click="commodityBtn"
        ></el-button>
      </el-input>
      <el-input
        style="width: 300px"
        :disabled="true"
        placeholder="请搜索类别信息"
        v-model="classNameList.name"
      >
        <template slot="prepend">类别</template>
        <el-button
          slot="append"
          icon="el-icon-search"
          @click="classBtn"
        ></el-button>
      </el-input>
      <el-input
        style="width: 300px"
        :disabled="true"
        placeholder="请搜索采购员信息"
        v-model="purchaseList.name"
      >
        <template slot="prepend">采购员</template>
        <el-button
          slot="append"
          icon="el-icon-search"
          @click="purchaseBtn"
        ></el-button>
      </el-input>
      <el-input
        style="width: 300px"
        :disabled="true"
        placeholder="请搜索仓库"
        v-model="warehouseList.name"
      >
        <template slot="prepend">仓库</template>
        <el-button
          slot="append"
          icon="el-icon-search"
          @click="warehouseBtn"
        ></el-button>
      </el-input>

      <el-date-picker
        value-format="yyyy-MM-dd"
        v-model="date"
        type="daterange"
        :clearable="false"
        range-separator="至"
        start-placeholder="开始日期"
        end-placeholder="结束日期"
      ></el-date-picker>
      <el-button
        size="small"
        type="primary"
        icon="el-icon-search"
        class="handleSearch"
        @click="handleSearch()"
        >查询</el-button
      >
      <el-button
        size="small"
        type="danger"
        class="handleSearch"
        icon="el-icon-delete"
        @click="empty()"
        >清空</el-button
      >
      <el-button
        type="info"
        size="small"
        icon="el-icon-tickets"
        @click="derive()"
        >导出</el-button
      >
    </div>
    <el-table
      max-height="600"
      :data="tableData"
      border
      style="width: 100%"
      empty-text="暂无数据"
      show-summary
      :summary-method="getSummaries"
    >
      <el-table-column prop="id" label="ID" align="center"></el-table-column>
      <el-table-column
        prop="goods_name"
        label="名称"
        align="center"
      ></el-table-column>
      <el-table-column
        prop="erp_goods_code"
        label="编码"
        align="center"
      ></el-table-column>
      <el-table-column
        prop="warehouse_name"
        label="仓库"
        align="center"
      ></el-table-column>
      <el-table-column
        prop="code"
        label="单据编号"
        align="center"
      ></el-table-column>
      <el-table-column
        prop="normal_desc"
        label="结算类型"
        align="center"
      ></el-table-column>
      <el-table-column
        prop="inp_num"
        label="数量"
        align="center"
      ></el-table-column>
       <el-table-column
        prop="qty"
        label="箱数"
        align="center"
      ></el-table-column>
      <el-table-column
        prop="stock_net_num"
        label="库存"
        align="center"
      ></el-table-column>
      <el-table-column
        prop="freeze_net_num"
        label="冻结数量"
        align="center"
      ></el-table-column>
      <el-table-column
        prop="inp_price"
        label="价格"
        align="center"
      ></el-table-column>
      <el-table-column
        prop="unit"
        label="单位"
        align="center"
      ></el-table-column>
      <el-table-column
        prop="type_desc"
        label="类型"
        align="center"
      ></el-table-column>
    </el-table>
    <!-- 分页 -->
    <pagination
      @page="handleSizeChange"
      @pagesize="handleCurrentChange"
      :pageData="pageData"
    ></pagination>
    <purchaseDialog
      :purchasedialog="purchasedialog"
      @tag="purchaseTag"
    ></purchaseDialog>
    <commodityDialog
      :commoditydialog="commoditydialog"
      @tag="commodityTag"
    ></commodityDialog>
    <classDialog :classdialog="classdialog" @tag="classTag"></classDialog>
    <warehouseselect
      :warehouseselect="warehouseselect"
      @tag="warehouseTag"
    ></warehouseselect>
  </div>
</template>
<script>
import { getnowDate } from "@/utils/index";
import Pagination from "@/components/pagination";
import PurchaseDialog from "@/components/grossMargin/purchaseDialog";
import SupplierSearch from "@/components/grossMargin/supplierSearch";
import CommodityDialog from "@/components/grossMargin/commodityDialog";
import ClassDialog from "@/components/grossMargin/classDialog";
import Warehouseselect from "@/components/commodity/warehouseselect";
import { inventory } from "@/api/commodity/commodityList";
import { getToken } from "@/utils/auth";

export default {
  components: {
    pagination: Pagination,
    purchaseDialog: PurchaseDialog,
    supplierSearch: SupplierSearch,
    commodityDialog: CommodityDialog,
    classDialog: ClassDialog,
    warehouseselect: Warehouseselect,
  },
  data() {
    return {
      //页面数据
      tableData: [],
      //分页
      pageData: {
        pageSizes: [10, 20],
        pageSize: 10,
        page: 1,
        total: null,
        layout: "total, sizes, prev, pager, next, jumper",
      },
      //默认时间
      normal_startime: getnowDate().startimer,
      normal_endtime: getnowDate().endtimer,
      search: {
        code: "",
        kind: "",
        classid: "",
        goods_id: "",
        buyer_code: "",
        start_time: "",
        end_time: "",
        is_normal: "",
      },
      normalType: [
        {
          value: -1,
          label: "全部",
        },
        {
          value: 0,
          label: "不结算",
        },
        {
          value: 1,
          label: "正常结算",
        },
        {
          value: 2,
          label: "非正常结算",
        },
      ],
      businessType: [
        {
          value: 10,
          label: "购货入库",
        },
        {
          value: 20,
          label: "盘点入库",
        },
        {
          value: 30,
          label: "退货入库",
        },
        {
          value: 40,
          label: "分货入库",
        },
        {
          value: 50,
          label: "赠品入库",
        },
        {
          value: 60,
          label: "加工入库",
        },
         {
          value: 102,
          label: "同步库存入库",
        },
      ],
      date: [],
      //dialog信息
      commoditydialog: {
        centerDialogVisible: false,
      },
      purchasedialog: {
        centerDialogVisible: false,
      },
      classdialog: {
        centerDialogVisible: false,
      },
      warehouseselect: {
        centerDialogVisible: false,
      },
      warehouseList: {
        name: "",
        code: "",
      },
      supplierNameList: {
        name: "",
        code: "",
      },
      commodityNameList: {
        name: "",
        code: "",
      },
      purchaseList: {
        name: "",
        code: "",
      },
      classNameList: {
        name: "",
        code: "",
      },
    };
  },
  created() {
    this.search.start_time = this.normal_startime;
    this.search.end_time = this.normal_endtime;
    if (this.normal_startime) {
      this.date.push(this.search.start_time);
    }
    if (this.normal_endtime) {
      this.date.push(this.search.end_time);
    }
    this.getData();
  },
  methods: {
    //获取页面数据
    getData() {
      let kindName = [];
      kindName.push(this.search.kind);
      let data = {
        limit: this.pageData.pageSize,
        page: this.pageData.page,
        kind: kindName.join(","),
        code: this.search.code,
        warehouse_code: this.warehouseList.code,
        classid: this.classNameList.code,
        goods_id: this.commodityNameList.code,
        buyer_code: this.purchaseList.code,
        starttime: this.search.start_time,
        endtime: this.search.end_time,
        is_normal: this.search.is_normal == -1 ? "" : this.search.is_normal,
      };

      inventory(data)
        .then((res) => {
          if (res.code == 0) {
            this.tableData = res.data.data;
            this.pageData.page = res.data.current_page;
            this.pageData.pagesize = res.data.per_page;
            this.pageData.total = res.data.total;
          }
        })
        .catch((err) => {
          console.log(err);
        });
    },
    commodityTag(data) {
      this.commodityNameList = {
        name: "",
        code: "",
      };
      console.log(data, 11111);
      let arrName = [];
      let arrCode = [];
      data.forEach((res) => {
        arrName.push(res.name);
        arrCode.push(res.id);
      });

      this.commodityNameList.name = arrName.join(",");
      this.commodityNameList.code = arrCode.join(",");
    },
    purchaseTag(data) {
      this.purchaseList = {
        name: "",
        code: "",
      };
      console.log(data, 11111);
      let arrName = [];
      let arrCode = [];
      data.forEach((res) => {
        arrName.push(res.name);
        arrCode.push(res.code);
      });
      this.purchaseList.name = arrName.join(",");
      this.purchaseList.code = arrCode.join(",");
    },
    classTag(data) {
      this.classNameList = {
        name: "",
        code: "",
      };
      console.log(data, 11111);
      let arrName = [];
      let arrCode = [];
      data.forEach((res) => {
        arrName.push(res.name);
        arrCode.push(res.id);
      });
      this.classNameList.name = arrName.join(",");
      this.classNameList.code = arrCode.join(",");
    },
    warehouseTag(data) {
      this.warehouseList = {
        name: "",
        code: "",
      };
      console.log(data, 11111);
      let arrName = [];
      let arrCode = [];
      data.forEach((res) => {
        arrName.push(res.name);
        arrCode.push(res.code);
      });
      this.warehouseList.name = arrName.join(",");
      this.warehouseList.code = arrCode.join(",");
    },
    //超级仓库搜索
    warehouseBtn() {
      this.warehouseselect = {
        centerDialogVisible: true,
      };
    },
    //超级商品搜索
    commodityBtn() {
      this.commoditydialog = {
        centerDialogVisible: true,
      };
    },
    //超级采购员搜索
    purchaseBtn() {
      this.purchasedialog = {
        centerDialogVisible: true,
      };
    },
    //超级类别搜索
    classBtn() {
      this.classdialog = {
        centerDialogVisible: true,
      };
    },

    //合计
    getSummaries(param) {
      const { columns, data } = param;
      const sums = [];
      columns.forEach((column, index) => {
        if (index === 0) {
          sums[index] = "合计";
          return;
        }
        const values = data.map((item) => Number(item[column.property]));
        if (!values.every((value) => isNaN(value))) {
          sums[index] = values.reduce((prev, curr) => {
            const value = Number(curr);
            if (!isNaN(value)) {
              return prev + curr;
            } else {
              return prev;
            }
          }, 0);
          if (index != 5 && index != 6 && index != 7 && index != 8) {
            sums[index] = "";
          } else {
            sums[index] = sums[index].toFixed(4);
          }
        } else {
          sums[index] = "";
        }
      });

      return sums;
    },
    // 本地搜索
    handleSearch() {
      if (this.date.length > 0) {
        this.search.start_time = this.date[0];
        this.search.end_time = this.date[1];
      }
      this.pageData.total = null;
      this.pageData.page = 1;
      this.getData();
    },
    //导出
    derive() {
      let kindName = [];
      kindName.push(this.search.kind);
      var kindvalue = kindName.join(",");
      this.$confirm("是否导出?", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning",
      })
        .then(() => {
          let link = document.createElement("a");
          if (this.date.length > 0) {
            this.search.start_time = this.date[0];
            this.search.end_time = this.date[1];
          }
          let is_normal =
            this.search.is_normal == -1 ? "" : this.search.is_normal;
          link.href =
            this.$axios.defaults.baseURL +
            "/api/goods/inventory?token=" +
            getToken() +
            "&kind=" +
            kindvalue +
            "&starttime=" +
            this.search.start_time +
            "&endtime=" +
            this.search.end_time +
            "&code=" +
            this.search.code +
            "&classid=" +
            this.classNameList.code +
            "&warehouse_code=" +
            this.warehouseList.code +
            "&goods_id=" +
            this.commodityNameList.code +
            "&is_normal=" +
            is_normal +
            "&buyer_code=" +
            this.purchaseList.code +
            "&export=1";
          link.click();
        })
        .catch(() => {
          this.$message({
            type: "info",
            message: "已取消导出",
          });
        });
    },
    //清空
    empty() {
      // this.date = "";

      this.date = [];
      this.date.push(this.normal_startime);
      this.date.push(this.normal_endtime);

      this.search = {
        classid: "",
        goods_id: "",
        buyer_code: "",
        kind: "",
        code: "",
        is_normal: "",
        start_time: this.normal_startime,
        end_time: this.normal_endtime,
      };
      this.classNameList.name = "";
      this.commodityNameList.name = "";
      this.purchaseList.name = "";
      this.classNameList.code = "";
      this.commodityNameList.code = "";
      this.purchaseList.code = "";
      this.warehouseList.name = "";
      this.warehouseList.code = "";
    },
    //每页多少条
    handleCurrentChange(val) {
      this.pageData.page = 1;
      this.pageData.pageSize = val;
      this.getData();
    },
    //选取页数
    handleSizeChange(page) {
      this.pageData.page = page;
      this.getData();
    },
  },
};
</script>
<style scoped>
.top {
  position: relative;
  margin-bottom: 5px;
}

.add {
  position: absolute;
  right: 0;
  top: 4px;
}
.pagination {
  text-align: right;
  padding: 10px;
}
</style>